package com.dot.dominion.database;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

import com.dot.dominion.R;
import com.dot.dominion.domain.Card;

public class DatabaseHandler extends DefaultHandler {
	private List<String> requestList;
	private ArrayList<Card> cardList;
	
	private String name = null;		//Card name
	private int pictureId = 0;		//Picture ID
	private int boardId = 0;		//Board-picture ID
	private int cost = 0;			//Card cost
	private int victoryPoint = 0;	//Victory point value
	private int treasureValue = 0;	//Treasure value
	private int addAction = 0;		//Additional actions
	private int addBuy = 0;			//Additional buys
	private int addCoin = 0;		//Aditional coins
	private int addDraw = 0;		//Aditional draws

	private boolean isCard = false;				//Card quantifier
	private boolean isName = false;				//Name quantifier
	private boolean isId = false;				//Picture ID quantifier
	private boolean isCost = false;				//Cost quantifier
	private boolean isVictoryPoint = false;		//Victory point quantifier
	private boolean isTreasureValue = false;	//Treasure quantifier
	private boolean isAddAction = false;		//Add. Action quantifier
	private boolean isAddBuy = false;			//Add. Buy quantifier
	private boolean isAddCoin = false;			//Add. Coin quantifier
	private boolean isAddDraw = false;			//Add. Draw quantifier
	private boolean skipCard = false;			//Skip this card or not
	
	public DatabaseHandler(List<String> listOfRequests) {
		requestList = listOfRequests;
		cardList = new ArrayList<Card>();
	}
	
	/*
	 * Reaction to the start of XMl tags
	 * 
	 * Flag the correct member field to store the appropriate
	 * data in.
	 */
	public void startElement(String uri, String localName,
			String qName, Attributes attributes) {
		
		String element = localName;
		
		if( element.equalsIgnoreCase("CARD") ) {
			isCard = true;
			skipCard = false;
		}
		
		if(! skipCard) {	//Only operate on a non-skipped card
			if( element.equalsIgnoreCase("NAME") )
				isName = true;
			if( element.equalsIgnoreCase("PICTURENAME") )
				isId = true;
			if( element.equalsIgnoreCase("COST") )
				isCost = true;
			if( element.equalsIgnoreCase("VICTORYPOINT") )
				isVictoryPoint = true;
			if( element.equalsIgnoreCase("TREASUREVALUE") )
				isTreasureValue = true;
			if( element.equalsIgnoreCase("ADDITIONALACTION") )
				isAddAction = true;
			if( element.equalsIgnoreCase("ADDITIONALBUY") )
				isAddBuy = true;
			if( element.equalsIgnoreCase("ADDITIONALCOIN") )
				isAddCoin = true;
			if( element.equalsIgnoreCase("ADDITIONALDRAW") )
				isAddDraw = true;
		}
	}
	
	
	/*
	 * Reaction to the end of XML tags
	 * 
	 * If it's the end of a Card tag, check if the card is
	 * skipped. Skip it if it is; otherwise, create the card
	 * and add it to the Card List
	 */
	public void endElement(String uri, String localName,
			String qName) {
		
		String element = localName;
		
		if( element.equalsIgnoreCase("CARD") ) {
			if(! skipCard) {
				Card card = new Card(name, pictureId, boardId, cost,
						victoryPoint, treasureValue);
		
				//add card
				cardList.add( card );
				
				//reset fields
				name = null;
				pictureId = 0;
				cost = victoryPoint = treasureValue = 0;
			}
			
			isCard = false;
			skipCard = false;
		}
	}
	
	/*
	 * Reaction to the XML values
	 * 
	 * Depending on what tag was flagged, we either store
	 * the name, cost, victory point value, or treasure value
	 * in local variables
	 */
	public void characters(char ch[], int start, int length) {
		
		if(! skipCard ) {
			String element = new String(ch, start, length);

			if(isName) {
				if( requestList == null )
					name = element;
				else if( requestList.contains(element.toLowerCase()) )
					name = element;
				else
					skipCard = true;
				
				isName = false;
			}
			else if(isId) {
				try {
					Class res = R.drawable.class;
					Field field = res.getField(element);
					pictureId = field.getInt(null);
					field = res.getField(element+"bv");
					boardId = field.getInt(null);
				} catch( Exception e ) {
					//IllegalAccessException
					//NoSuchFieldException
				}
				
				isId = false;
			}
			else if(isCost) {
				cost = (new Integer(element)).intValue();
				isCost = false;
			}
			else if( isVictoryPoint ) {
				victoryPoint = (new Integer(element)).intValue();
				isVictoryPoint = false;
			}
			else if( isTreasureValue ) {
				treasureValue = (new Integer(element)).intValue();
				isTreasureValue = false;
			}
			else if( isAddAction ) {
				addAction = (new Integer(element)).intValue();
				
				isAddAction = false;
			}
			else if( isAddBuy ) {
				addBuy = (new Integer(element)).intValue();
				
				isAddBuy = false;
			}
			else if( isAddCoin ) {
				addCoin = (new Integer(element)).intValue();
				
				isAddCoin = false;
			}
			else if( isAddDraw ) {
				addDraw = (new Integer(element)).intValue();
				
				isAddDraw = false;
			}
		}
	}
	
	/**
	 * Obtain the requested list of cards from the database
	 * 
	 * @return	The cards requested from the database
	 */
	public List<Card> getCardList() {
		return cardList;
	}
}
